<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="停车场编号" prop="parkCode">
        <el-select v-model="queryParams.parkCode" @keyup.enter.native="handleQuery"   placeholder="请选择停车场" filterable clearable size="small" >
          <el-option
            v-for="park in parkListOptions"
            :key="park.parkCode"
            :label="park.parkName"
            :value="park.parkCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:channelInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:channelInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:channelInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:channelInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="channelInfoList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="停车场编号" align="center" prop="parkCode" />
      <el-table-column label="停车场名称" align="center" prop="parkName" />
      <el-table-column label="通道名称" align="center" prop="entranceName" />
      <el-table-column label="摄像头ip" align="center" prop="ip" />
      <el-table-column label="通道类型" align="center" prop="entranceType" :formatter="entranceTypeFormat" />
      <el-table-column label="通道状态" align="center" prop="enable" :formatter="enableFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="350">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="controlCha(scope.row,0)"
          >开闸</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sort"
            @click="controlCha(scope.row,1)"
          >关闸</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-picture" v-if="scope.row.entranceType == 1"
            @click="showImg(scope.row.enterQrCode,1,scope.row)"
          >入口码</el-button>
          <el-button
            size="mini"
            type="text" v-if="scope.row.entranceType == 2"
            icon="el-icon-picture-outline"
            @click="showImg(scope.row.prePayQrCode,2,scope.row)"
          >预支付码</el-button>
          <el-button
            size="mini"
            type="text" v-if="scope.row.entranceType == 2"
            icon="el-icon-picture-outline-round"
            @click="showImg(scope.row.payQrCode,3,scope.row)"
          >支付码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:channelInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:channelInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  查看二维码  -->
    <el-dialog :title="imgTitle" :visible.sync="imgOpen" width="30%" append-to-body>
      <div>
        <img :src="imgUrl" alt="" style="width: 100%;height:100%  " />
      </div>
    </el-dialog>


    <!-- 添加或修改通道管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">
        <el-form-item label="停车场编码" prop="parkCode">
          <el-select v-model="form.parkCode" style="width: 100%"  placeholder="请选择停车场" filterable clearable size="small" >
            <el-option
              v-for="park in parkListOptions"
              :key="park.parkCode"
              :label="park.parkName"
              :value="park.parkCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="通道编码" prop="channelCode">
          <el-input v-model="form.channelCode" placeholder="请输入通道编码" />
        </el-form-item>
        <el-form-item label="摄像头ip" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入摄像头ip" />
        </el-form-item>
        <el-form-item label="通道名称" prop="entranceName">
          <el-input v-model="form.entranceName" placeholder="请输入通道名称" />
        </el-form-item>
        <el-form-item label="一体机状态" prop="ytjStatus">
          <el-select v-model="form.ytjStatus" clearable placeholder="请选择一体机状态" style="width: 100%">
            <el-option
              v-for="dict in ytjStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通道类型" prop="entranceType">
          <el-select v-model="form.entranceType" clearable placeholder="请选择通道类型" style="width: 100%">
            <el-option
              v-for="dict in entranceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通道状态" prop="enable">
          <el-select v-model="form.enable" clearable placeholder="请选择通道状态" style="width: 100%">
            <el-option
              v-for="dict in enableOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChannelInfo, getChannelInfo, delChannelInfo, addChannelInfo, updateChannelInfo, exportChannelInfo,getQRcode,controlCha } from "@/api/system/channelInfo";
import {getParkList} from "@/api/system/park";

export default {
  name: "ChannelInfo",
  components: {
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通道管理表格数据
      channelInfoList: [],
      // 弹出层标题
      title: "",

      imgOpen: false,
      imgTitle: "",
      imgUrl: "",
      // 是否显示弹出层
      open: false,
      // 通道类型字典
      entranceTypeOptions: [],
      //一体机状态
      ytjStatusOptions: [],
      // 通道状态字典
      enableOptions: [],
      // 是否自助开闸 0否 1是字典
      isSelfOpenGateOptions: [],
      // 结算类型字典
      settlementTypeOptions: [],
      // 无牌车通行模式字典
      noPlatePassModeOptions: [],
      // 无通道权限放行模式，0未设定，1手动验证放行，2自动验证放行字典
      noChannelModeOptions: [],
      // 临时车放行模式，0未设定，1手动验证放行，2自动验证放行字典
      normalCarPassModeOptions: [],
      // 本地vip车辆进场模式，0未设置，1手动确认放行，2自动确认放行字典
      localVipCarPassModeOptions: [],
      // 第三方vip车辆进场模式，0未设置，1手动确认放行，2自动确认放行字典
      thirdPartyVipCarPassModeOptions: [],
      // vip无在场放行模式，0未设定，1手动验证放行，2自动验证放行字典
      vipNotInPassModeOptions: [],
      // 黑名单通行模式，0未设定，1手动验证放行，2自动验证放行字典
      blacklistPassModeOptions: [],
      // 红名单通行模式，0未设定，1手动验证放行，2自动验证放行字典
      redListPassModeOptions: [],
      // 访客通行模式，0未设定，1手动验证放行，2自动验证放行字典
      visitorListPassModeOptions: [],
      // 共享车辆放行模式，0未设定，1手动验证放行，2自动验证放行字典
      shareListPassModeOptions: [],
      // 是否是根区域绑定的通道 0否 1 是字典
      isRootChannelOptions: [],
      // 动作类型 新增0修改1删除2 这里必须要填个值字典
      actionTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkCode: undefined,
      },
      // 表单参数
      form: {},
      // 停车场列表
      parkListOptions: [],
      // 表单校验
      rules: {
        parkCode:[
          {required:true,message:"停车场编号不能为空", trigger: "blur"}
        ],
        entranceName:[
          {required:true,message:"通道名称不能为空", trigger: "blur"}
        ],
        entranceType:[
          {required:true,message:"请选择通道类型", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getParkList();
    this.getDicts("channel_entrance_type").then(response => {
      this.entranceTypeOptions = response.data;
    });
    this.getDicts("channel_ytj_status").then(response => {
      this.ytjStatusOptions = response.data;
    });
    this.getDicts("channel_enable").then(response => {
      this.enableOptions = response.data;
    });
    this.getDicts("channel_is_self_open_gate").then(response => {
      this.isSelfOpenGateOptions = response.data;
    });
    this.getDicts("channel_settlement_type").then(response => {
      this.settlementTypeOptions = response.data;
    });
    this.getDicts("channel_no_plate_pass_mode").then(response => {
      this.noPlatePassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.noChannelModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.normalCarPassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.localVipCarPassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.thirdPartyVipCarPassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.vipNotInPassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.blacklistPassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.redListPassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.visitorListPassModeOptions = response.data;
    });
    this.getDicts("channel_no_channel_mode").then(response => {
      this.shareListPassModeOptions = response.data;
    });
    this.getDicts("channel_is_root").then(response => {
      this.isRootChannelOptions = response.data;
    });
    this.getDicts("channel_action_type").then(response => {
      this.actionTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询通道管理列表 */
    getList() {
      this.loading = true;
      listChannelInfo(this.queryParams).then(response => {
        this.channelInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getParkList(){
      let data={}
      getParkList(data).then(response => {
        this.parkListOptions = response.data;
      });
    },
    // 通道类型字典翻译
    entranceTypeFormat(row, column) {
      return this.selectDictLabel(this.entranceTypeOptions, row.entranceType);
    },
    // 一体机状态
    ytjStatusFormat(row, column) {
      return this.selectDictLabel(this.ytjStatusOptions, row.ytjStatus);
    },
    // 通道状态字典翻译
    enableFormat(row, column) {
      return this.selectDictLabel(this.enableOptions, row.enable);
    },
    // 是否自助开闸 0否 1是字典翻译
    isSelfOpenGateFormat(row, column) {
      return this.selectDictLabel(this.isSelfOpenGateOptions, row.isSelfOpenGate);
    },
    // 结算类型字典翻译
    settlementTypeFormat(row, column) {
      return this.selectDictLabel(this.settlementTypeOptions, row.settlementType);
    },
    // 无牌车通行模式字典翻译
    noPlatePassModeFormat(row, column) {
      return this.selectDictLabel(this.noPlatePassModeOptions, row.noPlatePassMode);
    },
    // 无通道权限放行模式，0未设定，1手动验证放行，2自动验证放行字典翻译
    noChannelModeFormat(row, column) {
      return this.selectDictLabel(this.noChannelModeOptions, row.noChannelMode);
    },
    // 临时车放行模式，0未设定，1手动验证放行，2自动验证放行字典翻译
    normalCarPassModeFormat(row, column) {
      return this.selectDictLabel(this.normalCarPassModeOptions, row.normalCarPassMode);
    },
    // 本地vip车辆进场模式，0未设置，1手动确认放行，2自动确认放行字典翻译
    localVipCarPassModeFormat(row, column) {
      return this.selectDictLabel(this.localVipCarPassModeOptions, row.localVipCarPassMode);
    },
    // 第三方vip车辆进场模式，0未设置，1手动确认放行，2自动确认放行字典翻译
    thirdPartyVipCarPassModeFormat(row, column) {
      return this.selectDictLabel(this.thirdPartyVipCarPassModeOptions, row.thirdPartyVipCarPassMode);
    },
    // vip无在场放行模式，0未设定，1手动验证放行，2自动验证放行字典翻译
    vipNotInPassModeFormat(row, column) {
      return this.selectDictLabel(this.vipNotInPassModeOptions, row.vipNotInPassMode);
    },
    // 黑名单通行模式，0未设定，1手动验证放行，2自动验证放行字典翻译
    blacklistPassModeFormat(row, column) {
      return this.selectDictLabel(this.blacklistPassModeOptions, row.blacklistPassMode);
    },
    // 红名单通行模式，0未设定，1手动验证放行，2自动验证放行字典翻译
    redListPassModeFormat(row, column) {
      return this.selectDictLabel(this.redListPassModeOptions, row.redListPassMode);
    },
    // 访客通行模式，0未设定，1手动验证放行，2自动验证放行字典翻译
    visitorListPassModeFormat(row, column) {
      return this.selectDictLabel(this.visitorListPassModeOptions, row.visitorListPassMode);
    },
    // 共享车辆放行模式，0未设定，1手动验证放行，2自动验证放行字典翻译
    shareListPassModeFormat(row, column) {
      return this.selectDictLabel(this.shareListPassModeOptions, row.shareListPassMode);
    },
    // 是否是根区域绑定的通道 0否 1 是字典翻译
    isRootChannelFormat(row, column) {
      return this.selectDictLabel(this.isRootChannelOptions, row.isRootChannel);
    },
    // 动作类型 新增0修改1删除2 这里必须要填个值字典翻译
    actionTypeFormat(row, column) {
      return this.selectDictLabel(this.actionTypeOptions, row.actionType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        communityId: undefined,
        cameraId: undefined,
        ip: undefined,
        parkGlobalId: undefined,
        channelCode: undefined,
        etcCode: undefined,
        entranceName: undefined,
        ytjSeq: undefined,
        ytjStatus: 0,
        entranceType: undefined,
        enable: undefined,
        isSelfOpenGate: undefined,
        settlementType: undefined,
        noPlatePassMode: undefined,
        noChannelMode: undefined,
        normalCarPassMode: undefined,
        localVipCarPassMode: undefined,
        thirdPartyVipCarPassMode: undefined,
        vipNotInPassMode: undefined,
        blacklistPassMode: undefined,
        redListPassMode: undefined,
        visitorListPassMode: undefined,
        shareListPassMode: undefined,
        dynamicVipToNormalMode: undefined,
        parkCode: undefined,
        voiceOpenPeriod: undefined,
        enterQrCode: undefined,
        prePayQrCode: undefined,
        payQrCode: undefined,
        qrCodeStr: undefined,
        customCode: undefined,
        areaId: undefined,
        isRootChannel: undefined,
        actionType: undefined,
        timeArray: undefined,
        deviceVolume: undefined,
        boxId: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.resetForm("form");
    },

    //开关闸
    controlCha(row,type){
      if(type == 0){  //开闸
        this.$confirm('确定要开闸通道['+row.entranceName+']吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          var params = {
            id:row.id,
            opType:0
          }
          return controlCha(JSON.stringify(params));
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("开闸成功");
        }).catch(() => {});
      }
      if (type == 1){ //关闸
        this.$confirm('确定要关闸通道['+row.entranceName+']吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          var params = {
            id:row.id,
            opType:1
          }
          return controlCha(JSON.stringify(params));
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("关闸成功");
        }).catch(() => {});
      }

    },

    //显示二维码
    showImg(imgUrl,type,row) {
      if (imgUrl !=null && imgUrl !=""){
        this.imgOpen = true;
        this.imgTitle = "二维码详情";
        this.imgUrl = imgUrl;
      }else {
        if (type == 1){ //生成入口码
            this.$confirm('确定生成入口码吗?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.loading = true;
              var params = {
                path: "pages/index/tem-app/tem-app?parkCode="+row.parkCode+"&channelSeq="+row.channelCode,
                id:row.id,
                type:0
              }
              return getQRcode(JSON.stringify(params));
            }).then(() => {
              this.loading = false;
              this.getList();
              this.msgSuccess(row.entranceName+"入口码生成成功，重新点击按钮查看");
            }).catch(() => {});
        }
        if (type == 2){ //生成预支付码
          this.$confirm('确定要生成排队缴费支付码吗?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            this.loading = true;
            var params = {
              path: "pages/index/getaway/getaway?parkCode="+row.parkCode+"&channelSeq=''",
              id:row.id,
              type:2
            }
            return getQRcode(JSON.stringify(params));
          }).then(() => {
            this.loading = false;
            this.getList();
            this.msgSuccess(row.entranceName+"缴费支付码生成成功，重新点击按钮查看");
          }).catch(() => {});
        }
        if (type == 3){ //生成支付码
          this.$confirm('确定要生成支付码吗?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            this.loading = true;
            var params = {
              path: "pages/index/ves-com-out/ves-com-out?parkCode="+row.parkCode+"&channelSeq="+row.channelCode,
              id:row.id,
              type:1
            }
            return getQRcode(JSON.stringify(params));
          }).then(() => {
            this.loading = false;
            this.getList();
            this.msgSuccess(row.entranceName+"支付码生成成功，重新点击按钮查看");
          }).catch(() => {});
        }
      }

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通道管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getChannelInfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改通道管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateChannelInfo(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChannelInfo(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除通道管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delChannelInfo(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有通道管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportChannelInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
